package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.ChannelConfigDTO;
import com.dkd.manage.domain.dto.ChannelDTO;
import com.dkd.manage.domain.vo.ChannelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 售货机货道Service业务层处理
 *
 * @author 久睡成瘾
 * @date 2025-10-13
 */
@Service
@Slf4j
public class ChannelServiceImpl implements IChannelService {
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     *
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id) {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     *
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel) {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel) {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel) {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     *
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids) {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     *
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id) {
        return channelMapper.deleteChannelById(id);
    }

    /**
     * 根据售货机编号查询该售货机货道详细信息
     *
     * @param innerCode 售货机编号
     * @return 结果
     */
    @Override
    public List<ChannelVO> getChannelVOListByInnerCode(String innerCode) {

        return channelMapper.getChannelVOListByInnerCode(innerCode);
    }

    /**
     * 配置货道信息
     *
     * @param configDTO 传来货道数据
     * @return 结果
     */
    @Override
    @Transactional
    public int configChannel(ChannelConfigDTO configDTO) {
        ChannelDTO[] channelDTOList = configDTO.getChannelList();
        //存储需要修改的货道集合
        List<Channel> channelList = new ArrayList<>();
        List<Channel> channelEditList = Arrays.stream(channelDTOList).map(dto -> {
            //1、先查询该售货机是否存在该货道
            //2、存在该货道将需要修改商品存为Channel对象
            Channel result = channelMapper.getChannelByInnerCodeAndChannelCode(dto.getInnerCode(), dto.getChannelCode());
            if (result != null) {
                //货道存在设置货道关联商品和更新日期
                result.setSkuId(dto.getSkuId());
                result.setUpdateTime(DateUtils.getNowDate());
                // 3、将Channel对象存入集合
                channelList.add(result);
            } else {
                log.error("货道不存在，设备编号={},货道编号={}", dto.getInnerCode(), dto.getChannelCode());
            }
            return result;
        }).collect(Collectors.toList());

        //4、根据Channel集合批量更新数据库
        int result = channelMapper.updateChannelBatch(channelEditList);
        return result;
    }
}
